package com.moqianfan.mcuassistant.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.TextView;

import com.moqianfan.mcuassistant.R;
import com.moqianfan.mcuassistant.adapter.CommDataAdapter;
import com.moqianfan.mcuassistant.adapter.ExpandAsmListAdapter;
import com.moqianfan.mcuassistant.adapter.ExpandCListAdapter;
import com.moqianfan.mcuassistant.adapter.ExpandMcuListAdapter;
import com.moqianfan.mcuassistant.bean.ASMCourse;
import com.moqianfan.mcuassistant.bean.CSource;
import com.moqianfan.mcuassistant.bean.McuCourse;
import com.moqianfan.mcuassistant.database.ASMCourseDao;
import com.moqianfan.mcuassistant.database.CSourceDao;
import com.moqianfan.mcuassistant.database.McuCourseDao;
import com.moqianfan.mcuassistant.utils.LogUtil;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/6/14.
 * 单片机教程 C函数库模块
 */

public class CourseActivity extends BaseActivity implements ExpandableListView.OnChildClickListener, AdapterView.OnItemClickListener {

    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.exp_list)
    ExpandableListView listMcu;
    @BindView(R.id.list_search)
    ListView listSearch;
    @BindView(R.id.edit_course_search)
    EditText editSearch;

    //公用大标题
    private List<String> heads;
    //单片机教程参数
    private List<McuCourse> allMcus;
    private McuCourseDao mcuCourseDao;
    private List<List<McuCourse>> mcuChilds;
    private CommDataAdapter<McuCourse> searchAdapter;
    private ExpandMcuListAdapter adapter;
    private List<McuCourse> searchItem;
    //C函数库参数
    private List<CSource> allCSources;
    private CSourceDao cSourceDao;
    private List<List<CSource>> csourceChilds;
    private List<CSource> cSourcesItem;
    private CommDataAdapter<CSource> csSearchAdapter;
    private ExpandCListAdapter cAdapter;
    //汇编教程
    private List<ASMCourse> allAsms;
    private ASMCourseDao asmCourseDao;
    private List<List<ASMCourse>> asmChilds;
    private List<ASMCourse> asmSearchItem;
    private CommDataAdapter<ASMCourse> asmSearchAdapter;
    private ExpandAsmListAdapter asmAdapter;

    private String TAG = "CourseActivity";
    private int flag = -1;

    @Override
    public void findViews() {
        setContentView(R.layout.activity_course);
    }

    @Override
    public void initViews() {
        String title = getIntent().getStringExtra("title");
        txtTitle.setText(title);
        heads = new ArrayList<>();
        switch (title) {
            case "单片机基础":
                flag = 0;
                allMcus = new ArrayList<>();
                mcuChilds = new ArrayList<>();
                searchItem = new ArrayList<>();
                mcuCourseDao = appConfig.getDaoSession().getMcuCourseDao();
                break;
            case "C函数库":
                flag = 1;
                allCSources = new ArrayList<>();
                csourceChilds = new ArrayList<>();
                cSourcesItem = new ArrayList<>();
                cSourceDao = appConfig.getDaoSession().getCSourceDao();
                break;
            case "汇编教程":
                flag = 2;
                allAsms = new ArrayList<>();
                asmChilds = new ArrayList<>();
                asmSearchItem = new ArrayList<>();
                asmCourseDao = appConfig.getDaoSession().getASMCourseDao();
                break;
        }

        listMcu.setOnChildClickListener(this);
        listSearch.setOnItemClickListener(this);
        editSearch.addTextChangedListener(watcher);
    }

    @Override
    public void initDatas() {
        switch (flag) {
            case 0:
                allMcus = mcuCourseDao.loadAll();
                Set<String> temp0 = new LinkedHashSet<>();
                for (McuCourse item : allMcus) {
                    LogUtil.w(TAG, item.toString());
                    temp0.add(item.getHead());
                }
                heads.clear();
                heads.addAll(temp0);
                List<McuCourse> tempMcus;
                mcuChilds.clear();
                for (String item : heads) {
                    LogUtil.w(TAG, "章节：" + item);
                    tempMcus = new ArrayList<>();
                    for (McuCourse item1 : allMcus) {
                        if (item1.getHead().equals(item))
                            tempMcus.add(item1);
                    }
                    mcuChilds.add(tempMcus);
                }
                if (null == adapter) {
                    adapter = new ExpandMcuListAdapter(heads, mcuChilds, this);
                    listMcu.setAdapter(adapter);
                } else {
                    adapter.setgData(heads);
                    adapter.setiData(mcuChilds);
                }
                break;
            case 1:
                allCSources = cSourceDao.loadAll();
                Set<String> temp1 = new LinkedHashSet<>();
                for (CSource item : allCSources) {
                    LogUtil.w(TAG, item.toString());
                    temp1.add(item.getHead());
                }
                heads.clear();
                heads.addAll(temp1);
                List<CSource> tempCS;
                for (String item : heads) {
                    LogUtil.w(TAG, "章节：" + item);
                    tempCS = new ArrayList<>();
                    for (CSource item1 : allCSources) {
                        if (item1.getHead().equals(item))
                            tempCS.add(item1);
                    }
                    csourceChilds.add(tempCS);
                }

                if (null == cAdapter) {
                    cAdapter = new ExpandCListAdapter(heads, csourceChilds, this);
                    listMcu.setAdapter(cAdapter);
                } else {
                    cAdapter.setgData(heads);
                    cAdapter.setiData(csourceChilds);
                }
                break;
            case 2:
                allAsms = asmCourseDao.loadAll();
                Set<String> temp2 = new LinkedHashSet<>();
                for (ASMCourse item : allAsms) {
                    LogUtil.w(TAG, item.toString());
                    temp2.add(item.getHead());
                }
                heads.clear();
                heads.addAll(temp2);
                List<ASMCourse> tempAsms;
                asmChilds.clear();
                for (String item : heads) {
                    LogUtil.w(TAG, "章节：" + item);
                    tempAsms = new ArrayList<>();
                    for (ASMCourse item1 : allAsms) {
                        if (item1.getHead().equals(item))
                            tempAsms.add(item1);
                    }
                    asmChilds.add(tempAsms);
                }
                if (null == asmAdapter) {
                    asmAdapter = new ExpandAsmListAdapter(heads, asmChilds, this);
                    listMcu.setAdapter(asmAdapter);
                } else {
                    asmAdapter.setgData(heads);
                    asmAdapter.setiData(asmChilds);
                }
                break;
        }
    }

    @OnClick({R.id.image_close})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.image_close:
                finish();
                break;
        }
    }

    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        Intent intent = new Intent();
        intent.setClass(CourseActivity.this, WebInfoActivity.class);
        switch (flag) {
            case 0:
                LogUtil.w(TAG, "点击：" + mcuChilds.get(groupPosition).get(childPosition).getTitle());
                intent.putExtra("title", mcuChilds.get(groupPosition).get(childPosition).getTitle());
                intent.putExtra("html", mcuChilds.get(groupPosition).get(childPosition).getHtml());
                intent.putExtra("head", mcuChilds.get(groupPosition).get(childPosition).getHead());
                intent.putExtra("desc", mcuChilds.get(groupPosition).get(childPosition).getHead());
                break;
            case 1:
                LogUtil.w(TAG, "点击：" + csourceChilds.get(groupPosition).get(childPosition).getTitle());
                intent.putExtra("title", csourceChilds.get(groupPosition).get(childPosition).getTitle());
                intent.putExtra("head", csourceChilds.get(groupPosition).get(childPosition).getHead());
                if (!TextUtils.isEmpty(csourceChilds.get(groupPosition).get(childPosition).getHtml()))
                    intent.putExtra("html", csourceChilds.get(groupPosition).get(childPosition).getHtml());
                else
                    intent.putExtra("url", csourceChilds.get(groupPosition).get(childPosition).getUrl());
                intent.putExtra("desc", csourceChilds.get(groupPosition).get(childPosition).getUrl());
                break;
            case 2:
                LogUtil.w(TAG, "点击：" + asmChilds.get(groupPosition).get(childPosition).getTitle());
                intent.putExtra("title", asmChilds.get(groupPosition).get(childPosition).getTitle());
                intent.putExtra("html", asmChilds.get(groupPosition).get(childPosition).getHtml());
                intent.putExtra("head", asmChilds.get(groupPosition).get(childPosition).getHead());
                intent.putExtra("desc", asmChilds.get(groupPosition).get(childPosition).getHead());
                break;
        }

        startActivity(intent);
        return true;
    }

    private TextWatcher watcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence s, int i, int i1, int i2) {

            if (!TextUtils.isEmpty(s.toString())) {
                listSearch.setVisibility(View.VISIBLE);
                listMcu.setVisibility(View.INVISIBLE);
                if (flag == 0) {
                    searchItem.clear();
                    for (McuCourse item : allMcus) {
                        if (item.getTitle().contains(s.toString())) {
                            searchItem.add(item);
                        }
                    }
                    if (null == searchAdapter) {
                        searchAdapter = new CommDataAdapter<McuCourse>(searchItem, R.layout.list_group) {
                            @Override
                            public void bindView(ViewHolder holder, McuCourse obj) {
                                holder.setText(R.id.list_content_item, obj.getTitle());
                            }
                        };
                        listSearch.setAdapter(searchAdapter);
                    } else {
                        searchAdapter.setmData(searchItem);
                    }
                } else if (flag == 1) {
                    cSourcesItem.clear();
                    for (CSource item : allCSources) {
                        if (item.getTitle().contains(s.toString()) || item.getInfo().contains(s.toString()))
                            cSourcesItem.add(item);
                    }

                    if (null == csSearchAdapter) {
                        csSearchAdapter = new CommDataAdapter<CSource>(cSourcesItem, R.layout.list_group) {
                            @Override
                            public void bindView(ViewHolder holder, CSource obj) {
                                holder.setText(R.id.list_content_item, obj.getTitle());
                            }
                        };
                        listSearch.setAdapter(csSearchAdapter);
                    } else {
                        csSearchAdapter.setmData(cSourcesItem);
                    }
                } else {
                    asmSearchItem.clear();
                    for (ASMCourse item : allAsms) {
                        if (item.getTitle().contains(s.toString())) {
                            asmSearchItem.add(item);
                        }
                    }
                    if (null == asmSearchAdapter) {
                        asmSearchAdapter = new CommDataAdapter<ASMCourse>(asmSearchItem, R.layout.list_group) {
                            @Override
                            public void bindView(ViewHolder holder, ASMCourse obj) {
                                holder.setText(R.id.list_content_item, obj.getTitle());
                            }
                        };
                        listSearch.setAdapter(asmSearchAdapter);
                    } else {
                        asmSearchAdapter.setmData(asmSearchItem);
                    }
                }
            } else {
                listMcu.setVisibility(View.VISIBLE);
                listSearch.setVisibility(View.GONE);
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    };

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent();
        intent.setClass(CourseActivity.this, WebInfoActivity.class);
        if (flag == 0) {
            intent.putExtra("title", searchItem.get(position).getTitle());
            intent.putExtra("html", searchItem.get(position).getHtml());
            intent.putExtra("head", searchItem.get(position).getHead());
            intent.putExtra("desc", searchItem.get(position).getHead());
        } else if (flag == 1) {
            intent.putExtra("title", cSourcesItem.get(position).getTitle());
            if (!TextUtils.isEmpty(cSourcesItem.get(position).getHtml()))
                intent.putExtra("html", cSourcesItem.get(position).getHtml());
            else
                intent.putExtra("url", cSourcesItem.get(position).getUrl());
            intent.putExtra("head", cSourcesItem.get(position).getHead());
            intent.putExtra("desc", cSourcesItem.get(position).getHead());
        } else {
            intent.putExtra("title", asmSearchItem.get(position).getTitle());
            intent.putExtra("html", asmSearchItem.get(position).getHtml());
            intent.putExtra("head", asmSearchItem.get(position).getHead());
            intent.putExtra("desc", asmSearchItem.get(position).getHead());
        }
        startActivity(intent);
    }
}
